package com.sec.bnjey.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.sec.bnjey.utils.RbLog;
import com.sec.bnjey.vo.DriveDetailEntity;

public class HttpPost {

	private static final String TAG = "HttpPost";
	public static final boolean ENABLE_HTTP_POST = true;

	private static final String CARMASTER_URL = "http://www.linkcrm.net/CRM/";

	private static NetworkEventListener mListener = null;

	public static final int NETWORK_DTC = 0;
	public static final int NETWORK_MILEAGE = 1;
	public static final int NETWORK_DRIVEINFO = 2;
	public static final int NETWORK_CARINFO = 3;
	public static final int NETWORK_LASTDRIVEINDEX = 4;

	public static String sendDtc(final String bt_id, final String phone,
			final int cnt, final String codes, NetworkEventListener listener) {

		mListener = listener;

		final StringBuilder builder = new StringBuilder();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {

					URL url = new URL(CARMASTER_URL + "crm_recv_dtc.jsp");
					HttpURLConnection http = (HttpURLConnection) url
							.openConnection();

					http.setDefaultUseCaches(false);
					http.setDoInput(true);
					http.setDoOutput(true);
					http.setRequestMethod("POST");
					http.setConnectTimeout(2000);

					http.setRequestProperty("content-type",
							"application/x-www-form-urlencoded");
					StringBuffer buffer = new StringBuffer();
					buffer.append("sn_id").append("=").append(bt_id)
							.append("&");
					buffer.append("phone").append("=").append(phone)
							.append("&");
					if (cnt > 0) {
						buffer.append("cnt").append("=").append(cnt)
								.append("&");
						buffer.append("codes").append("=").append(codes);
					} else {
						buffer.append("cnt").append("=").append(cnt);
					}

					RbLog.d(TAG, "sendDtc_length= " + buffer.length());

					OutputStreamWriter outStream = new OutputStreamWriter(http
							.getOutputStream(), "UTF-8");
					PrintWriter writer = new PrintWriter(outStream);
					writer.write(buffer.toString());

					writer.flush();

					InputStreamReader tmp = new InputStreamReader(http
							.getInputStream(), "UTF-8");
					BufferedReader reader = new BufferedReader(tmp);

					String str;
					while ((str = reader.readLine()) != null) {
						builder.append(str + "\n");
					}
					RbLog.d(TAG, "sendDtc : " + builder.toString());
					mListener.onNetworkEvent(NETWORK_DTC, builder.toString());

				} catch (MalformedURLException e) {
					RbLog.d(TAG, "sendDtc : MalformedURLException : " + e);
				} catch (IOException e) {
					RbLog.d(TAG, "sendDtc : IOException : " + e);
				} // try

			}
		}).start();

		return null;

	}

	public static String sendMileage(final String bt_id, final String phone,
			final int mileage, NetworkEventListener listener) {

		mListener = listener;

		final StringBuilder builder = new StringBuilder();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					URL url = new URL(CARMASTER_URL + "crm_recv_mileage.jsp");
					HttpURLConnection http = (HttpURLConnection) url
							.openConnection();

					http.setDefaultUseCaches(false);
					http.setDoInput(true);
					http.setDoOutput(true);
					http.setRequestMethod("POST");
					http.setConnectTimeout(2000);

					http.setRequestProperty("content-type",
							"application/x-www-form-urlencoded");
					StringBuffer buffer = new StringBuffer();

					buffer.append("sn_id").append("=").append(bt_id)
							.append("&");
					buffer.append("phone").append("=").append(phone)
							.append("&");
					buffer.append("mileage").append("=").append(mileage);

					RbLog.d(TAG, "2" + buffer.length());
					OutputStreamWriter outStream = new OutputStreamWriter(http
							.getOutputStream(), "UTF-8");
					PrintWriter writer = new PrintWriter(outStream);

					writer.write(buffer.toString());
					writer.flush();

					InputStreamReader tmp = new InputStreamReader(http
							.getInputStream(), "UTF-8");
					BufferedReader reader = new BufferedReader(tmp);
					String str;
					while ((str = reader.readLine()) != null) {
						builder.append(str + "\n");
					}
					RbLog.d(TAG, "sendMileage : " + builder.toString());

					mListener.onNetworkEvent(NETWORK_MILEAGE,
							builder.toString());
				} catch (MalformedURLException e) {
					RbLog.d(TAG, "sendDtc : MalformedURLException : " + e);
				} catch (IOException e) {
					RbLog.d(TAG, "sendDtc : IOException : " + e);
				} // try

			}
		}).start();

		return null;
	}

	/**
	 * send drive info
	 * 
	 * @param bt_id
	 * @param phone
	 * @param info
	 * @param listener
	 * @return
	 */
	public static String sendDriveInfo(final String bt_id, final String phone,
			final DriveDetailEntity info, NetworkEventListener listener) {

		mListener = listener;

		final StringBuilder builder = new StringBuilder();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {

					URL url = new URL(CARMASTER_URL + "crm_recv_driveinfo.jsp");
					HttpURLConnection http = (HttpURLConnection) url
							.openConnection();

					http.setDefaultUseCaches(false);
					http.setDoInput(true);
					http.setDoOutput(true);
					http.setRequestMethod("POST");
					http.setConnectTimeout(2000);

					http.setRequestProperty("content-type",
							"application/x-www-form-urlencoded");
					StringBuffer buffer = new StringBuffer();
					buffer.append("sn_id").append("=").append(bt_id)
							.append("&");
					buffer.append("phone").append("=").append(phone)
							.append("&");
					buffer.append(info.toParamString());

					// RbLog.d(TAG,buffer.length());
					RbLog.d(TAG, buffer.toString());

					OutputStreamWriter outStream = new OutputStreamWriter(http
							.getOutputStream(), "UTF-8");
					PrintWriter writer = new PrintWriter(outStream);
					writer.write(buffer.toString());

					writer.flush();

					InputStreamReader tmp = new InputStreamReader(http
							.getInputStream(), "UTF-8");
					BufferedReader reader = new BufferedReader(tmp);

					String str;
					while ((str = reader.readLine()) != null) {
						builder.append(str + "\n");
					}
					RbLog.d(TAG, "sendDriveInfo : " + builder.toString());

					mListener.onNetworkEvent(NETWORK_DRIVEINFO,
							builder.toString());

				} catch (MalformedURLException e) {
					RbLog.d(TAG, "sendDtc : MalformedURLException : " + e);
				} catch (IOException e) {
					RbLog.d(TAG, "sendDtc : IOException : " + e);
				} // try

			}
		}).start();

		return builder.toString();
	}

	public static String sendCarInfo(final String bt_id, final String phone,
			final String car, NetworkEventListener listener) {

		mListener = listener;
		RbLog.d(TAG, "sendCarInfo");

		final StringBuilder builder = new StringBuilder();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					URL url = new URL(CARMASTER_URL
							+ "crm_recv_vehicleinfo.jsp");
					HttpURLConnection http = (HttpURLConnection) url
							.openConnection();
					http.setDefaultUseCaches(false);
					http.setDoInput(true);
					http.setDoOutput(true);
					http.setRequestMethod("POST");
					http.setConnectTimeout(2000);
					http.setRequestProperty("content-type",
							"application/x-www-form-urlencoded");
					StringBuffer buffer = new StringBuffer();
					buffer.append("sn_id").append("=").append(bt_id)
							.append("&");
					buffer.append("phone").append("=").append(phone)
							.append("&");
					buffer.append("vehicleinfo").append("=").append(car);

					RbLog.d(TAG, car);

					OutputStreamWriter outStream = new OutputStreamWriter(http
							.getOutputStream(), "UTF-8");
					PrintWriter writer = new PrintWriter(outStream);
					writer.write(buffer.toString());

					writer.flush();

					InputStreamReader tmp = new InputStreamReader(http
							.getInputStream(), "UTF-8");
					BufferedReader reader = new BufferedReader(tmp);

					String str;
					while ((str = reader.readLine()) != null) {
						builder.append(str + "\n");
					}
					RbLog.d(TAG, "sendvehicleInfo : " + builder.toString());

					mListener.onNetworkEvent(NETWORK_CARINFO,
							builder.toString());
				} catch (MalformedURLException e) {
					RbLog.d(TAG, "sendDtc : MalformedURLException : " + e);
				} catch (IOException e) {
					RbLog.d(TAG, "sendDtc : IOException : " + e);
				} // try

			}
		}).start();

		return builder.toString();

	}

	public static String requestLastDriveIndex(final String bt_id,
			final String phone, NetworkEventListener listener) {

		mListener = listener;
		RbLog.d(TAG, "requestLastDriveIndex phone : " + phone + " bt_id : "
				+ bt_id);

		final StringBuilder builder = new StringBuilder();
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					URL url = new URL(CARMASTER_URL
							+ "crm_recv_lastindexinfo.jsp");
					HttpURLConnection http = (HttpURLConnection) url
							.openConnection();
					http.setDefaultUseCaches(false);
					http.setDoInput(true);
					http.setDoOutput(true);
					http.setRequestMethod("POST");
					http.setConnectTimeout(2000);
					http.setRequestProperty("content-type",
							"application/x-www-form-urlencoded");
					StringBuffer buffer = new StringBuffer();
					buffer.append("sn_id").append("=").append(bt_id)
							.append("&");
					buffer.append("phone").append("=").append(phone)
							.append("&");
					// buffer.append("vehicleinfo").append("=").append(car);

					// RbLog.d(TAG, car);

					OutputStreamWriter outStream = new OutputStreamWriter(http
							.getOutputStream(), "UTF-8");
					PrintWriter writer = new PrintWriter(outStream);
					writer.write(buffer.toString());

					writer.flush();

					InputStreamReader tmp = new InputStreamReader(http
							.getInputStream(), "UTF-8");
					BufferedReader reader = new BufferedReader(tmp);

					String str;
					while ((str = reader.readLine()) != null) {
						builder.append(str + "\n");
					}
					RbLog.d(TAG, "sendvehicleInfo : " + builder.toString());

					mListener.onNetworkEvent(NETWORK_LASTDRIVEINDEX,
							builder.toString());
				} catch (MalformedURLException e) {
					RbLog.d(TAG, "sendDtc : MalformedURLException : " + e);
				} catch (IOException e) {
					RbLog.d(TAG, "sendDtc : IOException : " + e);
				} // try

			}
		}).start();

		return builder.toString();

	}
}
